// 火锅小店经营游戏数据管理工具类

// 店铺等级配置
export const SHOP_LEVELS = [
    { level: 1, name: '小摊', minReputation: 0, maxStaff: 2 },
    { level: 2, name: '小店', minReputation: 100, maxStaff: 4 },
    { level: 3, name: '连锁店', minReputation: 300, maxStaff: 6 },
    { level: 4, name: '品牌店', minReputation: 600, maxStaff: 8 },
    { level: 5, name: '旗舰店', minReputation: 1000, maxStaff: 12 }
]

// 员工类型配置
export const STAFF_TYPES = [
    {
        type: 'chef',
        name: '主厨',
        baseSalary: 100,
        skills: ['特制麻辣底料', '秘制酱料', '创新菜品'],
        efficiency: 1.5
    },
    {
        type: 'waiter',
        name: '服务员',
        baseSalary: 50,
        skills: ['热情服务', '快速上菜', '顾客关怀'],
        efficiency: 1.2
    },
    {
        type: 'manager',
        name: '店长',
        baseSalary: 200,
        skills: ['店铺管理', '员工培训', '成本控制'],
        efficiency: 2.0
    }
]

// 食材配置
export const INGREDIENTS = [
    {
        id: 'beef',
        name: '牛肉',
        price: 50,
        quality: 1,
        category: 'meat'
    },
    {
        id: 'lamb',
        name: '羊肉',
        price: 60,
        quality: 1,
        category: 'meat'
    },
    {
        id: 'shrimp',
        name: '虾滑',
        price: 40,
        quality: 1,
        category: 'seafood'
    },
    {
        id: 'vegetable',
        name: '时令蔬菜',
        price: 20,
        quality: 1,
        category: 'vegetable'
    }
]

// 底料配置
export const SOUP_BASES = [
    {
        id: 'spicy',
        name: '麻辣底料',
        price: 100,
        quality: 1,
        ingredients: ['beef', 'lamb']
    },
    {
        id: 'mushroom',
        name: '菌菇底料',
        price: 80,
        quality: 1,
        ingredients: ['vegetable']
    },
    {
        id: 'seafood',
        name: '海鲜底料',
        price: 120,
        quality: 1,
        ingredients: ['shrimp']
    }
]

// 计算店铺收入
export const calculateShopIncome = (shopData) => {
    const baseIncome = 100
    const levelBonus = SHOP_LEVELS.find(l => l.level === shopData.level).minReputation / 100
    const staffBonus = shopData.staff.reduce((sum, staff) => {
        const staffType = STAFF_TYPES.find(t => t.type === staff.type)
        return sum + staffType.efficiency
    }, 0) / shopData.staff.length
    
    return Math.floor(baseIncome * (1 + levelBonus) * staffBonus)
}

// 计算店铺支出
export const calculateShopExpense = (shopData) => {
    const staffSalary = shopData.staff.reduce((sum, staff) => {
        const staffType = STAFF_TYPES.find(t => t.type === staff.type)
        return sum + staffType.baseSalary
    }, 0)
    
    const ingredientCost = shopData.ingredients.reduce((sum, ing) => {
        const ingredient = INGREDIENTS.find(i => i.id === ing.id)
        return sum + ingredient.price * ing.quantity
    }, 0)
    
    return staffSalary + ingredientCost
}

// 检查是否可以升级店铺
export const canUpgradeShop = (shopData) => {
    const currentLevel = SHOP_LEVELS.find(l => l.level === shopData.level)
    const nextLevel = SHOP_LEVELS.find(l => l.level === shopData.level + 1)
    
    if (!nextLevel) return false
    
    return shopData.reputation >= nextLevel.minReputation
}

// 升级店铺
export const upgradeShop = (shopData) => {
    if (!canUpgradeShop(shopData)) return false
    
    shopData.level++
    return true
}

// 检查是否可以雇佣新员工
export const canHireStaff = (shopData) => {
    const currentLevel = SHOP_LEVELS.find(l => l.level === shopData.level)
    return shopData.staff.length < currentLevel.maxStaff
}

// 雇佣员工
export const hireStaff = (shopData, staffType) => {
    if (!canHireStaff(shopData)) return false
    
    const staff = STAFF_TYPES.find(t => t.type === staffType)
    if (!staff) return false
    
    shopData.staff.push({
        type: staffType,
        name: generateStaffName(staffType),
        level: 1,
        experience: 0
    })
    
    return true
}

// 生成员工名字
const generateStaffName = (staffType) => {
    const surnames = ['张', '李', '王', '刘', '陈']
    const names = ['明', '华', '强', '伟', '芳']
    const surname = surnames[Math.floor(Math.random() * surnames.length)]
    const name = names[Math.floor(Math.random() * names.length)]
    return surname + name
}

// 购买食材
export const buyIngredient = (shopData, ingredientId, quantity) => {
    const ingredient = INGREDIENTS.find(i => i.id === ingredientId)
    if (!ingredient) return false
    
    const cost = ingredient.price * quantity
    if (shopData.coins < cost) return false
    
    const existingIngredient = shopData.ingredients.find(i => i.id === ingredientId)
    if (existingIngredient) {
        existingIngredient.quantity += quantity
    } else {
        shopData.ingredients.push({
            id: ingredientId,
            quantity: quantity
        })
    }
    
    shopData.coins -= cost
    return true
}

// 调配底料
export const makeSoupBase = (shopData, soupBaseId) => {
    const soupBase = SOUP_BASES.find(s => s.id === soupBaseId)
    if (!soupBase) return false
    
    // 检查食材是否足够
    for (const ingredientId of soupBase.ingredients) {
        const ingredient = shopData.ingredients.find(i => i.id === ingredientId)
        if (!ingredient || ingredient.quantity < 1) return false
    }
    
    // 消耗食材
    for (const ingredientId of soupBase.ingredients) {
        const ingredient = shopData.ingredients.find(i => i.id === ingredientId)
        ingredient.quantity--
    }
    
    // 增加底料库存
    const existingBase = shopData.soupBases.find(s => s.id === soupBaseId)
    if (existingBase) {
        existingBase.quantity++
    } else {
        shopData.soupBases.push({
            id: soupBaseId,
            quantity: 1
        })
    }
    
    return true
}

// 完成任务奖励
export const completeTask = (shopData, task) => {
    if (task.completed) return false
    
    shopData.coins += task.reward
    task.completed = true
    return true
}

// 保存游戏数据
export const saveGameData = (shopData) => {
    try {
        uni.setStorageSync('shopGameData', JSON.stringify(shopData))
        return true
    } catch (e) {
        console.error('保存游戏数据失败:', e)
        return false
    }
}

// 加载游戏数据
export const loadGameData = () => {
    try {
        const data = uni.getStorageSync('shopGameData')
        return data ? JSON.parse(data) : null
    } catch (e) {
        console.error('加载游戏数据失败:', e)
        return null
    }
} 